package model;

import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Vector;

import com.mysql.jdbc.Connection;
import com.mysql.jdbc.Statement;

public class BeanQuestions {

		private Vector<Question> listeQuestion;
				
		public BeanQuestions(){
			// initialiser listeQuestion avec BDD
			listeQuestion = new Vector<Question>();
		}
		
		public Question getFirstQuestion(){
			return listeQuestion.get(0);
		}
		
		public Question getLastQuestion(){
			return listeQuestion.lastElement();
		}
		
		public void createListeQuestion() throws ClassNotFoundException {
			
			String url = "jdbc:mysql://localhost:3306/Base";
			String utilisateur = "root";
			String motdepasse = "mysqlroot";
			Connection connexion = null;
		try{
			Class.forName("com.mysql.jdbc.Driver");
        	connexion = (Connection) DriverManager.getConnection(url, utilisateur, motdepasse);
        	Statement query = (Statement) connexion.createStatement();
//        	query.executeQuery("SET NAMES latin1");
        	Statement query2 = (Statement) connexion.createStatement();
//        	query.executeQuery("SET NAMES UTF8");
//        	query2.executeQuery("SET NAMES UTF8");
//        	query.executeQuery("SET questions 'utf8'");
//        	query2.executeQuery("SET NAMES 'utf8'");
        	ResultSet rs = query.executeQuery("SELECT * FROM questions");

	        	while(rs.next())
	        	{
	        		Question question = new Question();
//		        			System.out.println("question : "+ rs.getString("intitule"));
	            	question.setId(rs.getInt("idQ"));
	        		question.setIntitule(rs.getString("intitule"));
	        		question.setDescription(rs.getString("description"));
 
	        		ResultSet rs2 = query2.executeQuery("SELECT * FROM reponses WHERE idQ ="+question.getId());
	        		Vector<Reponse> listereponses = new Vector<Reponse>();
	        		while(rs2.next())
	        		{

//	        			System.out.println("Réponse existante !");
	        			Reponse reponse = new Reponse();
	        			reponse.setIntitule(rs2.getString("intitule"));
	        			reponse.setSolution(rs2.getBoolean("solution"));
//		        			System.out.println(reponse.getIntitule()+" : "+reponse.isSolution());
	        			
	        			listereponses.add(reponse);
	        			question.setListeReponse(listereponses);
	        		}
	        		
	        		this.listeQuestion.add(question);
//		        		System.out.println("Ajout de la question :"+question.getId()+" : "+question.getIntitule());
	        		
	        	}
	        	
//	        	setListeQuestion(listeQuestion);
	
	        	
				} catch ( SQLException e ) {
	        	System.out.println("Pas de question");
	            /* Gérer les éventuelles erreurs ici */
				} finally {
		            if ( connexion != null )
		                try {
		                    /* Fermeture de la connexion */
		                    connexion.close();
		                	} catch ( SQLException ignore ) {
		                    /* Si une erreur survient lors de la fermeture, il suffit de l'ignorer. */
		          }
	        }
			
		
		}

		public void setListeQuestion(Vector<Question> listeQuestion) {
			this.listeQuestion = listeQuestion;
		}

		public int getSize() {
			return listeQuestion.size();
		}
		
		public void add(Question question)
		{
			listeQuestion.add(question);
		}
		
		public Vector<Question> getListeQuestion() {
			return listeQuestion;
		}
		
		public Question getQuestionSuivante(Question questionCourante){
			Question questionSuivante = null;
			if(listeQuestion.contains(questionCourante)){
				int index = listeQuestion.indexOf(questionCourante);
				index++;
				if (index<listeQuestion.size()){
					if(listeQuestion.get(index) != null)
						questionSuivante = listeQuestion.get(index); //null si plus de questions apr�s
				}
				
			}
				
			return questionSuivante;
		}
}
